// hooks/useSharedData.ts
import { provide, inject, ref, computed } from 'vue';

const SharedDataKey = Symbol('shared-data');

export function useProvideSharedData() {
  const state = ref({});

  const initState = (val) => {
    state.value = val;
  };

  // 定义安全的更新方法
  const updateMethods = (key, val) => {
    state.value = { ...state.value, [key]: val };
  };

  // 提供上下文对象
  provide(SharedDataKey, {
    state: state,
    update: updateMethods,
  });

  return { initState };
}

export function useInjectSharedData() {
  const { state, update } = inject(SharedDataKey);

  const res = computed(() => state.value);

  return [res, update];
}
